// (c) Velikobratov Maxim, 2012
// Foodexpert Clientside Application.
//  Version 0.1

const S_GET_URL_NEW_RECIPES = "/rpc/recipes/get_new";
const I_ITEMS_PER_PAGE = 10;

var curPos = 0;

$('#bt_favourite').click(function(){
    $.get('/rpc/recipes/get_list', function(data) {
      $('.result').html(data);
    });
});

$('#bt_profexit').click(function(){
  $.get('{{ url }}', function(data) {
    window.location="/fe/";
  });
});

$('#bt_new').click(function(){
  getNewItemsList(0, I_ITEMS_PER_PAGE);
});

function getRecipListHTML(jsonDoc) {
  res = ""
  for (r in jsonDoc) {
    $('<div />', {html: jsonDoc[r].descript}).text();
	  res += "<a href='/recipe/?name='" + jsonDoc[r].name +
      "'> <div class='recipe'><div class='rname'>" + jsonDoc[r].name + "</div>" +
      "<div class='descript'>" + $('<div />', {html: jsonDoc[r].descript}).text() + "</div>"+
      jsonDoc[r].author + "</div> </a>";
  }
  return res;
}

function getNextItems(){
  curPos += I_ITEMS_PER_PAGE;
  fromItem = curPos;
  itemsCount = I_ITEMS_PER_PAGE;
  getNewItemsList(fromItem, itemsCount);
}

function getPrevItems(){
  curPos -= I_ITEMS_PER_PAGE;
  if (curPos < 0) curPos = 0;
  getNewItemsList(curPos, I_ITEMS_PER_PAGE);
}

function getNewItemsList(fromItem, itemsCount){
  $.get(S_GET_URL_NEW_RECIPES + '?from=' + fromItem +"&count=" + itemsCount, function(data) {
    $('.result').html(data + "<div><span id='bt_prev'> << </span> | <span id='bt_next'> >> </span></div>");
    $('#bt_prev').click(function(){
      getPrevItems();
    });
    $('#bt_next').click(function(){
      getNextItems();
    });
  });
  $('h1').html("New Recipes");
}

